package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.entity.union.honors.UnionHistoryHonorsRecordData;
import com.yanqu.road.entity.union.honors.eHonorsActivityType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.config.GoodsMgr;
import com.yanqu.road.server.manager.union.CrossUnionLockerMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_UNION_UPLOAD_REWARD, desc = "商帮获得奖励")
public class CrossUnionUploadRewardCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        CrossUnionProto.CrossUnionUploadRewardReqMsg reqMsg = CrossUnionProto.CrossUnionUploadRewardReqMsg.parseFrom(packet.getBytes());

        Object lock = CrossUnionLockerMgr.getGourpLocker(serverId);
        List<Long> recordList = new ArrayList<>();

        Map<String, CrossUnionInfo> unionInfoMap = new HashMap<>();
        synchronized (lock) {
            for (CrossUnionProto.CrossUnionRewardTemp rewardTemp : reqMsg.getRewardList()) {
                Property reward = PropertyHelper.parseStringToProperty(rewardTemp.getReward());
                int type = rewardTemp.getType();
                CrossUnionMember member = CrossUnionMgr.getUnionMember(rewardTemp.getUnionUid(), rewardTemp.getUserId());
                CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(rewardTemp.getUnionUid());
                if(member == null || unionInfo == null){
                    continue;
                }
                CrossUserBaseInfo userBaseInfo = CrossUserMgr.getCrossUserBaseInfo(member.getUserId());
                if(userBaseInfo == null){
                    continue;
                }
                int son = rewardTemp.getSon();
                unionInfoMap.put(unionInfo.getUnionUid(), unionInfo);
                for (Map.Entry<Integer, BigInteger> entry : reward.getGoods().entrySet()) {

                    int goodsId = entry.getKey();
                    long num = entry.getValue().longValue();
                    if (GoodsMgr.isUnionFlag(goodsId) || GoodsMgr.isUnionFlagParts(goodsId)) {

                        CrossUnionMgr.addUnionFlag(rewardTemp.getUnionUid(), goodsId);

                        CrossUnionMgr.sendUnionChangeBroadcast(eUnionSyncType.Normal.getValue(), unionInfo, null, null, 0);

                        recordList.add(rewardTemp.getRecordId());
                    } else if (GameConfig.GAME_ITEM_UNION_WEALTH == goodsId) {
                        if(type == 0){
                            CrossUnionMgr.removeUnionWealth(userBaseInfo, unionInfo, num, null, son);
                        }else if(type == 1){
                            CrossUnionMgr.addUnionWealth(userBaseInfo, unionInfo, num, null, son);
                        }
                        recordList.add(rewardTemp.getRecordId());
                    } else if (GameConfig.GAME_ITEM_UNION_EXP == goodsId){
                        if(type == 0){

                        }else if(type == 1){
                            CrossUnionMgr.addUnionExp(userBaseInfo, unionInfo.getUnionUid(), num, null, son);
                        }
                        recordList.add(rewardTemp.getRecordId());
                    } else if (GameConfig.GAME_ITEM_UNION_CONTRIBUTION == goodsId){
                        if(type == 0){
                            CrossUnionMgr.removeContribution(num, unionInfo, member, son);
                        }else if(type == 1){
                            CrossUnionMgr.addContribution(num, unionInfo, member, son);
                        }
                        recordList.add(rewardTemp.getRecordId());
                    } else if (GoodsMgr.isHonorScore(goodsId)){
                        //荣誉积分道具
                        GoodsInfo goodsInfo = GoodsMgr.getGoodsInfo(goodsId);
                        if(goodsInfo != null){
                            eHonorsActivityType honorsActivityType = eHonorsActivityType.getHonorsType(goodsInfo.getParamList().get(0).intValue());
                            if(honorsActivityType != null){
                                UnionHistoryHonorsRecordData recordData = new UnionHistoryHonorsRecordData();
                                recordData.setHonors((int)num);
                                recordData.setActivityType(honorsActivityType.getActivityType());
                                recordData.setActivityChildType(honorsActivityType.getActivityChildType());
                                recordData.setUnionUid(unionInfo.getUnionUid());
                                CrossUnionHonorsMgr.addUnionHonorsPointData(recordData);
                                recordList.add(rewardTemp.getRecordId());
                            }
                        }
                    }
                }
            }
            if(recordList.size() > 0){
                CrossUnionProto.CrossUnionUploadRewardRespMsg.Builder respMsg = CrossUnionProto.CrossUnionUploadRewardRespMsg.newBuilder();
                respMsg.addAllRecordId(recordList);
                MessageHelper.sendPacket(serverId, 0, YanQuMessageUtils.buildMessage(Protocol.S_UNION_UPLOAD_REWARD_FROM_CROSS, respMsg));

                for (Map.Entry<String, CrossUnionInfo> infoEntry : unionInfoMap.entrySet()) {
                    CrossUnionMgr.sendUnionChangeBroadcast(eUnionSyncType.Normal.getValue(), infoEntry.getValue(), null, null, 0);
                }
            }
        }
    }

    @Override
    public void sendData() {

    }
}
